package chenji.yourticket.datacenter;

import java.io.IOException;
import java.util.Set;
import java.util.TreeSet;

import org.bukkit.inventory.ItemStack;

import chenji.yourticket.configuration.ConfigException;
import chenji.yourticket.configuration.KConfiguration;
import chenji.yourticket.model.ModelInfo;

public class PlayerData extends KConfiguration {
	String PlayerName;

	public PlayerData(String PlayerName) throws ConfigException {
		super("PlayerData/" + PlayerName+".yml", false,"成功创建玩家存档-"+PlayerName,"无法建立玩家存档-"+PlayerName);
		this.PlayerName = PlayerName;
	}

	@Override
	public void loadConfig() {

	}

	public ModelInfo readModeInfo(String Path) {
		if (!(this.getConfig().contains(Path))) {
			return null;
		}
		long date = this.getConfig().getLong(Path + ".CreateDate");
		ItemStack Item = this.getConfig().getItemStack(Path + ".Item");
		return new ModelInfo(date, Item, Path);
	}

	public void setItem(String Path, ItemStack i) {
		if (!(this.getConfig().contains(Path))) {
			return;
		}
		this.getConfig().set(Path + ".Item", i);
		this.saveConfiguration();
	}

	public void saveModelInfo(ModelInfo m) {
		this.getConfig().set(m.getName() + ".CreateDate", m.getCreateDate());
		this.getConfig().set(m.getName() + ".Item", m.getItem());
		this.saveConfiguration();

	}

	private void saveConfiguration() {
		try {
			this.saveConfig();
		} catch (IOException e) {
			try {
				throw new ConfigException(e, "保存玩家数据失败：" + this.PlayerName);
			} catch (ConfigException e1) {
				e1.printStackTrace();
			}
		}

	}

	public void removeModel(String name) {
		this.getConfig().set(name, null);
		this.saveConfiguration();
		
	}

	public TreeSet<ModelInfo> getTreeSet() {
		TreeSet<ModelInfo> mf = new TreeSet<ModelInfo>();
		Set<String> pathset = this.getConfig().getKeys(false);
		for(String path:pathset){
			ModelInfo mi = this.readModeInfo(path);
			mf.add(mi);
		}
		return mf;
	}

	public boolean contains(String message) {
		return this.getConfig().contains(message);
	}
	public int getAmount(){
		Set<String> s = this.getConfig().getKeys(false);
		if(s==null){return 0;}
		return s.size();
	}

}
